Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: separate data into staticData, encodedLengths, dynamicData in getRecord #1532

Merged
merged 8 commits into from
Sep 18, 2023

Conversation

dk1a
Copy link
Contributor

@dk1a dk1a commented Sep 17, 2023

fixes #1518

@changeset-bot
Copy link

changeset-bot bot commented Sep 17, 2023

🦋 Changeset detected

Latest commit: 659a56f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 29 packages
Name Type
@latticexyz/store Major
@latticexyz/world Major
@latticexyz/cli Major
@latticexyz/dev-tools Major
@latticexyz/react Major
@latticexyz/store-indexer Major
@latticexyz/store-sync Major
@latticexyz/abi-ts Major
@latticexyz/block-logs-stream Major
@latticexyz/common Major
@latticexyz/config Major
create-mud Major
@latticexyz/ecs-browser Major
@latticexyz/faucet Major
@latticexyz/gas-report Major
@latticexyz/network Major
@latticexyz/noise Major
@latticexyz/phaserx Major
@latticexyz/protocol-parser Major
@latticexyz/recs Major
@latticexyz/schema-type Major
@latticexyz/services Major
@latticexyz/solecs Major
solhint-config-mud Major
solhint-plugin-mud Major
@latticexyz/std-client Major
@latticexyz/std-contracts Major
@latticexyz/store-cache Major
@latticexyz/utils Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@dk1a dk1a force-pushed the dk1a/get-separated-data branch from bfbade3 to eabf4f4 Compare September 18, 2023 08:11
.changeset/hip-tables-check.md Outdated Show resolved Hide resolved
@@ -277,13 +304,13 @@ library Vector {
}

/** Tightly pack full data using this table's field layout */
function encode(int32 x, int32 y) internal pure returns (bytes memory) {
function encode(int32 x, int32 y) internal pure returns (bytes memory, PackedCounter, bytes memory) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it help at all gas wise to name these return values and use them in the function body?

function encode(int32 x, int32 y) internal pure returns (bytes memory _staticData, PackedCounter, bytes memory) {
  _staticData = encodeStatic(x, y);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tested on Callbacks and Vector2 but it doesn't seem to make a difference. Intuitively I'd also assume that this shouldn't affect bytecode

@@ -521,54 +521,31 @@ library StoreCore {
bytes32 tableId,
bytes32[] memory keyTuple,
FieldLayout fieldLayout
) internal view returns (bytes memory) {
) internal view returns (bytes memory staticData, PackedCounter encodedLengths, bytes memory dynamicData) {
Copy link
Member

@holic holic Sep 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we're now passing around this tuple a lot, do you think we'd get any value from encoding this as a struct? or would that make it less convenient, since we don't always use all three values?

struct ValueData {
  bytes staticData;
  PackedCounter encodedLengths;
  bytes dynamicData;
}

edit: nevermind, talked myself out of this idea

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might help in a few tests. Otherwise not sure, it's not used much outside codegen, and codegen is fine either way. Importing a struct isn't very convenient, but neither is a big tuple

Copy link
Member

@holic holic Sep 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see a lot of folks asking in help channel about stack too deep errors when dealing with lots of variables, is this anything to consider?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it would reduce vars from 3 to 1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be worth it to support more variables in generated libraries? Since the functions are not used a lot outside codegen I think the slightly decreased dev ex of having to import a struct for the return data is fine

edit: nevermind, talked myself out of this idea

curious why?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I talked myself out of it because there's a bunch of cases where we only carry around e.g. staticData or encodedLengths+dynamicData and it wouldn't make sense to carry them in a "partial" struct. So thought it'd be clearer to carry around just the things you need (explicit vars, even if it means 3 for the case of all data)

Copy link
Member

@alvrs alvrs Sep 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the main advantage of using the struct is in the set function, where I believe we'd currently run into "stack too deep" issues for tables where num keys + num fields is greater than 7 (because the set function has 5 additional variables besides the key+fields). That might be worked around if we make the version of setter that uses the table's data struct a "first class citizen" instead of internally calling the version with arguments.

In any case, i think we can do this as a follow up since it involves refactors to code unrelated to this PR. Opened an issue to track here: #1535 and #1536

${fieldNamePrefix}${field.name} = ${renderDecodeDynamicFieldPartial(field)};
`;
result += `
/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we indent this one level (and all the backticks below) for readability?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, I went with this style for all renderers so just sticking to it here. If we wanna change it I'd rather make a separate PR and change it everywhere

Copy link
Member

@holic holic Sep 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already did this: #1474
just wanna keep it consistent with the new format

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh right I missed that one, yeah all the other files are good and I was just focused on record, will fix

alvrs
alvrs previously approved these changes Sep 18, 2023
Copy link
Member

@alvrs alvrs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Let's do the struct refactor in a follow up (tracked in #1535 and #1536). Only nits left are the indentation mentioned by @holic and changing the changeset back to major.

@dk1a dk1a merged commit ae340b2 into main Sep 18, 2023
9 checks passed
@dk1a dk1a deleted the dk1a/get-separated-data branch September 18, 2023 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

separate data into staticData, encodedLengths, dynamicData in Store getter functions
3 participants